

list1=[1,2,"a",True,[1,2]]

print(f"{list1=}")

list2 = list()
list3 = list(range(1,10))
list4 = list("abcde")

print(f"{list2=}")
print(f"{list3=}")
print(f"{list4=}")

print("索引访问",list1[2])
print("索引访问",list1[-2])

# 切片
sub_list = list3[3:5]
print(f"{sub_list=}")
print(f"{list3=}")

sub_list2 = list3[2:-3]
print(f"{sub_list2=}")

sub_list3 = list3[1:10:2]
print(f"{sub_list3=}")

# sub_list4 = list3[::-1]
sub_list4= list3.reverse()
print(f"{sub_list4=}")
print(f"{list3=}")

list3.sort()
print(f"排序默认是升序{list3=}")

list3.sort(reverse= True)
print(f"排序默认是降序{list3=}")

sub_list6 = list3
# 深拷贝，在内存中会分配新的空间用于存放集合数据
sub_list5 = list3[:]
sub_list7 =list3.copy()

print(f"{sub_list5=}")
print("比较两个序列是否相同",list3 == sub_list5)
print("两个序列是否是指向同一个地址中的元素",list3 is sub_list5)
print("两个序列是否是指向同一个地址中的元素",list3 is sub_list6)

# list是可变对象，插入，删除，追加，修改
print(f"删除前{list1=}")
del list1[2]
print(f"删除后{list1=}")


print(f"删除前{list1=}")
del list1[1:3]
print(f"删除后{list1=}")


print(f"append前{list1=}")
list1.append([3,4])
list1.append("abc")
print(f"append后{list1=}")

print(f"extend前{list1=}")
list1.extend([3,4])
list1.extend("abc")
print(f"extend后{list1=}")


print(f"insert前{list1=}")
list1.insert(0,[3,4])
list1.insert(2,"abc")
print(f"insert后{list1=}")

print(f"pop前{list1=}")
pop_obj1=list1.pop()
pop_obj2=list1.pop(0)
print(f"pop后{list1=}")
print(f"{pop_obj1=}")
print(f"{pop_obj2=}")


print(f"remove前{list1=}")
rm_obj1=list1.remove('a')
rm_obj2=list1.remove([1,2])
print(f"remove后{list1=}")
print(f"{rm_obj1=}")
print(f"{rm_obj2=}")

list1.clear()
print(f"clear后{list1=}")

list6=['C9','a6','c8']
list6.sort(key=lambda s : s[1])
print(f"{list6=}")

# 列表推导式，用于创建列表
list7 = [ num for num in range(1,10) if num%3==0 ]

print(f"{list7=}")
# list=[]
# for num in range(1,10):
#     list.append(num)